package com.dragon.logical.service.supplies.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.supplies.IProductCategoryService;
import com.dragon.repository.entity.ProductCategory;
import com.dragon.repository.mapper.ProductCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品类别表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-09-11
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Autowired
    ProductCategoryMapper categoryMapper;

    @Override
    public ProductCategory saveCategory(ProductCategory convert) {
        categoryMapper.insert(convert);
        return convert;
    }

    @Override
    public void updateCategory(ProductCategory category){
        ProductCategory productCategory = categoryMapper.selectById(category.getId());
        if (productCategory == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        categoryMapper.updateById(category);
        // 修改下级状态
        List<ProductCategory> categoryList = categoryMapper.selectList(new QueryWrapper<ProductCategory>().lambda().eq(ProductCategory::getPid, category.getId()));
        categoryList.forEach(cate -> {
            cate.setStatus(category.getStatus());
            categoryMapper.updateById(cate);
        });

    }

    @Override
    public List<ProductCategory> categoryList(String search) {
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StrUtil.isNotBlank(search), ProductCategory::getName,search);
        List<ProductCategory> categoryList = categoryMapper.selectList(wrapper);
        return createTree(categoryList);
    }

    @Override
    public void deleteCategory(Integer id) {
        List<ProductCategory> categoryList = categoryMapper.selectList(new QueryWrapper<ProductCategory>()
                .lambda().eq(ProductCategory::getPid, id));
        if (categoryList.size() !=0 ){
            throw new CustomException(ResultCode.ADMIN_RELEVANCY);
        }else {
            categoryMapper.deleteById(id);
        }
    }

    /**
     *
     * 构造树形结构
     */
    public static List<ProductCategory> createTree(List<ProductCategory> sourceList){
        List<ProductCategory> list = new ArrayList<>();
        for (ProductCategory category : sourceList) {
            //找到根节点进行处理，找下一级节点
            if(null == category.getPid()){
                //把所有的根节点放到一个list
                list.add(findChildren(category,sourceList));
            }
        }

        return list;
    }
    public static ProductCategory findChildren(ProductCategory productCategory, List<ProductCategory> list){
        //这个方法是在找productCategory的所有子节点,然后返回productCategory
        for (ProductCategory category : list) {
            if(productCategory.getId().equals(category.getPid())){
                if(productCategory.getChildren() == null){
                    productCategory.setChildren(new ArrayList<>());
                }
                // 把这个节点 作为新的根节点继续向下构造树,再把构造的结果作为productCategory的子节点
                productCategory.getChildren().add(findChildren(category,list));
            }
        }

        return productCategory;
    }

}
